package com.yida.auth.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.yida.auth.form.LoginBody;
import com.yida.auth.form.RegisterBody;
import com.yida.auth.service.BasicDataService;
import com.yida.auth.service.SysLoginService;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.JwtUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.sign.RsaUtils;
import com.yida.common.security.auth.AuthUtil;
import com.yida.common.security.service.TokenService;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.system.api.model.LoginUser;

/**
 * token 控制
 * 
 * @author yida
 */
@RestController
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    public BasicDataService basicDataService;

    // @PostMapping("login")
    // public R<?> login(@RequestBody LoginBody form) throws Exception {
    // LoginUser userInfo = null;
    // String passwordInfo = RsaUtils.decryptByPrivateKey(form.getPassword());
    // if (!"admin".equals(form.getUsername())) {
    // // 获取手机校验开关
    // String codeEnabled = basicDataService.getPhoneCodeEnabled();
    // if ("true".equals(codeEnabled)) {
    // userInfo = sysLoginService.login(form.getUsername(), passwordInfo, form.getPhonenumber(),form.getSmsCode());
    // }else {
    // userInfo = sysLoginService.login(form.getUsername(), passwordInfo);
    // }
    // }
    // if (userInfo == null && "admin".equals(form.getUsername())) {
    // userInfo = sysLoginService.login(form.getUsername(), passwordInfo);
    // }
    //
    // if (userInfo == null) {
    // return R.fail("无效登陆操作!");
    // }
    //
    // // 获取登录token
    // return R.ok(tokenService.createToken(userInfo));
    // }

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form) throws Exception {
        LoginUser userInfo = null;
        String passwordInfo = RsaUtils.decryptByPrivateKey(form.getPassword());
        // 获取手机校验开关
        String codeEnabled = basicDataService.getPhoneCodeEnabled();
        if ("admin".equals(form.getUsername()) || "false".equals(codeEnabled)) {
            userInfo = sysLoginService.login(form.getUsername(), passwordInfo);
        }
        // 获取用户为空，并且不是管理员，且手机校验开关开启走全部权限校验
        if (userInfo == null && !"admin".equals(form.getUsername()) && "true".equals(codeEnabled)) {
            userInfo = sysLoginService.login(form.getUsername(), passwordInfo, form.getPhonenumber(), form.getSmsCode());
        }
        if (userInfo == null) {
            return R.fail("无效登陆操作!");
        }
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody) {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }
}
